export default {
  data() {
    return {
      Password: '', //验证码
      second: 60, //验证码60S
      showText: true, //重新发送验证码
      isDisabled: true, //下一步-灰色
      codeError: false, //验证码错误的时候显示的灰色
      mobile: '', //电话号码
      showmobile: '', //页面显示带**的电话号码
      overtime: false, //过期按钮
      interval: null, //倒计时
      isMobile: null, //验证码登录
      invitationCode: '', //邀请码
      seconds: 60,
      // 倒计时
      tips: '',
      codenum: '',
      platform: '', //设备平台信息
      purchaseinformation: ''
    }
  },
  computed: {
    btnActive() {
      if (this.codeError || this.overtime) {
        return false
      }
      return true
    }
  },

  onLoad(e) {
    //电话号码中间4位变成**
    this.mobile = e.mobileName
    this.showmobile = e.mobileName.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2')
    this.sendCode(0) //发送验证码
    if (e.isMobile === 'true') {
      this.isMobile = true
    } else if (e.isMobile === 'false') {
      this.isMobile = false
    } //验证码登录
    this.invitationCode = e.invitationCode
  },

  onShow() {

  },

  mounted() {
    // 获取设备信息
    const systemInfo = uni.getSystemInfoSync()
    console.log(systemInfo)
    let plat = ''
    // #ifdef APP-PLUS
    plat = '_APP'
    //  #endif
    // #ifdef H5
    plat = '_H5'
    //  #endif
    // #ifdef MP-WEIXIN
    plat = '_WEIXIN'
    //  #endif
    this.platform = systemInfo.platform
    if (systemInfo.brand) {
      this.platform = this.platform + '_' + systemInfo.brand
    }
    if (systemInfo.model) {
      this.platform = this.platform + '_' + systemInfo.model
    }
    this.platform = this.platform + plat
  },

  methods: {
    //倒计时
    codeChange(text) {
      // console.log(text)
      this.tips = text
    },
    end() {
      // this.$u.toast('倒计时结束')
    },
    start() {
      // this.$u.toast('已发送')
    },
    tipsbtns(ss) {
      if (ss === '重新获取验证码') {
        //重新获取验证码
        this.sendCode()
      } else if (ss === '获取验证码') {
        this.sendCode()
      } else {
        // console.log(234)
      }
    },
    //发送验证码
    async sendCode(type) {
      // if (type === 1) { //第一次发送
      //   this.Password = '' //清空验证码
      //   this.isDisabled = true //灰色下一步
      //   this.overtime = false //过期
      // }
      // 模拟向后端请求验证码
      uni.showLoading({
        title: '正在获取验证码'
      })

      try {
        const data = await this.$api.user.SendVerficationCode({
          mobile: this.mobile
        })
        this.Password = '' //清空验证码
        this.isDisabled = true //灰色下一步
        this.overtime = false //过期

        this.codenum = data.code
        setTimeout(() => {
          uni.hideLoading()
          if (this.codenum === 200) {
            if (data.data.isSuccess) {
              this.$refs.uCode.start()
            }
          }
        }, 1000)
        if (this.codenum === -4) {
          uni.showToast({
            title: '验证码已发送，请注意查看短信信息，不要频繁请求',
            icon: 'none'
          })
        } else {
          if (!data.data.isSuccess) {
            uni.showToast({
              title: '验证码超出同模板同号码天发送上限',
              icon: 'none'
            })
          }
        }

      } catch (err) {
        console.log(err)
        uni.hideLoading()
      }
    },
    //实时监测验证码是否输入完毕
    changeInput(e) {
      if (e.length === 6) { //验证码输入完毕，并且验证码是正确返回的
        this.isDisabled = false //高亮
        this.Password = e
      } else {
        this.isDisabled = true //灰色
      }
      this.codeError = false
    },

    //保存登录信息
    loginSuccess(param) {
      // this.$store.commit('user/setUserInfo',param)
      // this.$store.commit('setUserInfo',param)
      // 登录信息-vuex
      this.$store.commit('setModel', param)
      //token存储-缓存
      uni.setStorageSync('token', param.token)
      //用户信息-缓存
      uni.setStorageSync('Ztc_C_USER_INFO', param)
    },

    //点击下一步 验证码登录
    async nextStep() {
      try {
        let bussinessid = uni.getStorageSync('bussinessid') || ''
        let busstrue = uni.getStorageSync('busstrue') || ''
        let reqObj = {
          mobile: this.mobile, //用户名
          loginCode: this.Password, //	验证码/密码
          inviteCode: bussinessid || this.invitationCode, //商户id或邀请码
          imei: this.$store.state.imei, //IMEI
          LoginType: 0, //手机号验证码登录传0
          login_Platform: this.platform, //设备平台信息-仅注册需要
        }
        if (bussinessid) {
          reqObj.ShareType = 3
        }
        const res = await this.$api.user.ClientLogin(reqObj)
        this.isDisabled = true //点击提交之后，隐藏高亮按钮
        clearInterval(this.interval) //清除计时器
        this.interval = null
        this.loginSuccess(res.data) //保存
        if (res.code === 200) {
          if (bussinessid) {
            this.purchaseinformation = uni.getStorageSync('purchaseinformation')
            if (this.purchaseinformation !== '') {
              if (busstrue) {
                //true进入洗车券支付
                this.AddSelfServiceCarWashDiscountOrder() //false没有发显示弹框，true已发放不显示
              }

            }
            uni.removeStorageSync('showmoButton')
            uni.removeStorageSync('bussinessid')
            uni.removeStorageSync('purchaseinformation')
            // uni.reLaunch({
            //   url: '/pages_commodity/selfWash/carwash_globalche/carwash_globalche'
            // })
            return
          }
          const storeId = uni.getStorageSync('storeId')
          if (storeId) {
            uni.removeStorageSync('storeId')
            uni.reLaunch({
              url: '/pages_commodity/lineUp/lineUp?storeId=' + storeId
            })
            return
          }
          const existingOrder = uni.getStorageSync('existingOrder')
          if (existingOrder) {
            uni.removeStorageSync('existingOrder')
            uni.reLaunch({
              url: '/pages_commodity/selfWash/existingOrder?id=' + existingOrder
            })
            return
          }
          const signData = uni.getStorageSync('signData')
          if (signData && signData.id) {
            uni.removeStorageSync('signData')
            uni.reLaunch({
              url: `/pages_common/sign/sign?state=${signData.state}&id=${signData.id}`
            })
            return
          }

          uni.setStorage({
            key: 'launchFlag',
            data: true,
          })
          if (res.data.isNew) {
            uni.reLaunch({
              url: '/pages_logins/passwordLogin/passwordLogin?type=' + res.data.isNew
            })
          } else {
            this.$commen.loginSuccess()

            // uni.switchTab({
            //   url: '/pages/index/index'
            // })

            // uni.navigateBack({
            //   delta: 2
            // })

          }
        }
      } catch (err) {
        console.log('捕获错误', err)
      }

    },

    getUserCompany() {
      const _this = this
      this.$api.user.getUserCompany().then(res => {
        if (res.data.name) {
          _this.$store.commit('user/setCompanyInfo', {
            name: res.data.name,
            isJoinCompany: !res.data.isNew
          })
          uni.navigateTo({
            url: '/pages_login/authenticationPersonal/authenticationPersonal?companyName=' + res.data.name
          })
        } else {
          uni.navigateTo({
            url: '/pages_login/register/register'
          })
        }
      })
    },

    //获取用户信息
    async getUserInfo() {
      try {
        const res = await this.$api.user.getUserInfo()
        uni.setStorageSync('userInfoDetail', res.data)
        this.$store.commit('setUserInfoDetail', res.data)
      } catch (e) {
        //TODO handle the exception
      }
    },
    //提交自助洗车会员卡订单
    async AddSelfServiceCarWashDiscountOrder(types) {
      try {
        let data = {
          self_Service_Car_Wash_Card_Id: this.purchaseinformation.self_Service_Car_Wash_Card_Id,
          purchase_Channel: 3, // 购买途径 1：平台 2：线下  3：推广（从落地页跳转的）
          self_Service_Car_Wash_Station_Id: this.purchaseinformation.self_Service_Car_Wash_Station_Id, //凤英传的商户端的门店ID
        }
        const res = await this.$api.selfServiceCarWash.AddSelfServiceCarWashDiscountOrder(data)
        uni.hideLoading()
        let carmoney = this.purchaseinformation.price
        if (res.code === 200) {
          uni.redirectTo({
            url: `/pages_commodity/newPay/newPay?orderNo=${res.data}&type=selfServiceCarWashCoupon&price=${carmoney}&machineId=${''}`
            // url: `/pages_commodity/payment/payment?orderNo=${res.data}&type=selfServiceCarWashCoupon&price=${carmoney}&machineId=${''}`
          })
        }
      } catch (e) {
        console.log(e)
      }
    },
  }
}
